package 力扣.链表;

public class 删除排序链表中的重复元素82 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(1);
        head.next.next = new ListNode(1);
        head.next.next.next = new ListNode(2);
        head.next.next.next.next = new ListNode(3);
//        head.next.next.next.next.next = new ListNode(4);
//        head.next.next.next.next.next.next = new ListNode(5);

        ListNode listNode = deleteDuplicates2(head);
    }

    //递归增强版：适用于三个数的比较，不用考虑太多的边界条件并且更加快速

    //头节点为空或者头节点的下一个节点为空时，这时没有重复节点

    //若头节点和下一个节点相同，则跳过所有重复的节点
    //若头节点和下一个节点不同，则递归调用 判断下一个节点是否重复
    public static ListNode deleteDuplicates2(ListNode head) {
          if (head == null || head.next == null){
              return head;
          }
          if (head.next.val == head.val){
              while (head.next != null && head.next.val == head.val){
                  head = head.next;
              }
              return deleteDuplicates2(head.next);//当前节点不可用，递归进入下一个节点
          }else {
              head.next = deleteDuplicates2(head.next);
              return head;//递归调用下一个节点
          }
    }








    public ListNode deleteDuplicates3(ListNode head) {
        if (head == null ||head.next == null){
             return head;
         }
         if (head.val == head.next.val){
             while (head.next != null && head.val == head.next.val){
                 head = head.next;
             }
             return  deleteDuplicates3(head.next);
         }else {
             head.next = deleteDuplicates3(head.next);
             return head;
         }
    }
}
